package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 医院设置表 前端控制器
 */
@RestController
@RequestMapping("/admin/hospset")
@Api(tags = "医院设置相关信息")
@Slf4j
public class HospitalSetController {


    @Autowired
    private HospitalSetService hospitalSetService;


    //1.增
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet){
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000));
        hospitalSet.setSignKey(encrypt);
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }
    //2.批量删除
    @DeleteMapping("/batchRemove")
    public R batchRemove(@RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }
    //3.改：回显
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Long id){

        HospitalSet hospitalSet = null;
        hospitalSet = hospitalSetService.getById(id);

        return R.ok().data("item",hospitalSet);
    }

    //3.改：
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setUpdateTime(new Date());
        hospitalSetService.updateById(hospitalSet);

        return R.ok();
    }
    //3.带查询条件的分页查询
    @PostMapping("/{pageNum}/{pageSize}")
    public R page(@PathVariable Integer pageNum,
                  @PathVariable Integer pageSize,
                  @RequestBody HospitalSetQueryVo hospitalSetQueryVo){

        Page<HospitalSet> page=new Page<HospitalSet>(pageNum,pageSize);
        LambdaQueryWrapper<HospitalSet> queryWrapper=new LambdaQueryWrapper<HospitalSet>();
        queryWrapper.like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()),HospitalSet::getHosname,hospitalSetQueryVo.getHosname());
        queryWrapper.eq(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()),HospitalSet::getHoscode,hospitalSetQueryVo.getHoscode());
        hospitalSetService.page(page,queryWrapper);
        return R.ok().data("total",page.getTotal()).data("items",page.getRecords());
    }

    @PutMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "查询所有的医院设置信息")
    @GetMapping("/all")
    public R findAll(){
        return R.ok().data("list",hospitalSetService.list());
    }


    //paramType:query|header|path
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "医院id",required = false,defaultValue = "1",dataType = "Long",paramType = "path")}
    )

    /**
     * @Api(tags=""):对接口做说明的
     * @ApiOperation(value)：对接口中的方法做说明的
     * @ApiParam(name="",value="",required=,defaultValue=)：对接口中的方法做说明的,
     * @ApiImplicitParams和@ApiImplicitParam:(name = "id",value = "医院id",required = false,defaultValue = "1",dataType = "Long",paramType = "path")
     *    paramType:query|header|path
     * @ApiModel
     * @ApiModelProperty:
     *
     * @ApiResponse(code,message,response=)
     * @ApiResponses
     */
    @ApiOperation(value = "根据医院id删除医院设置信息")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable Long id){
        boolean b = hospitalSetService.removeById(id);
        if (b){
            return R.ok();
        }else{
            return R.error();
        }

    }


}

